/*
 *
 *  * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 *
 *
 */

package com.iwindplus.base.rabbit;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.iwindplus.base.rabbit.domain.property.RabbitProperty;
import com.iwindplus.base.rabbit.service.RabbitDynamicRegistry;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMq配置.
 *
 * @author zengdegui
 * @since 2020/4/28
 */
@Slf4j
@Configuration
@AutoConfigureAfter(RabbitAutoConfiguration.class)
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "enabled", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(RabbitProperty.class)
public class RabbitConfiguration {

    @Resource
    private AmqpAdmin amqpAdmin;

    @Resource
    private RabbitProperty property;

    @Resource
    private ObjectMapper objectMapper;

    /**
     * 动态创建队列、交换机初始化器.
     *
     * @return RabbitDynamicRegistry
     */
    @Bean
    public RabbitDynamicRegistry rabbitDynamicRegistry() {
        final RabbitDynamicRegistry rabbitDynamicRegistry = new RabbitDynamicRegistry(this.amqpAdmin, this.property);
        log.info("RabbitDynamicRegistry={}", rabbitDynamicRegistry);
        return rabbitDynamicRegistry;
    }

    /**
     * jackson 转换器转换消息.
     *
     * @return Jackson2JsonMessageConverter
     */
    @Bean
    public Jackson2JsonMessageConverter jackson2JsonMessageConverter() {
        Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter(this.objectMapper);
        log.info("Jackson2JsonMessageConverter={}", jackson2JsonMessageConverter);
        return jackson2JsonMessageConverter;
    }

}
